package com.baizhi.service.impl;

import com.baizhi.dao.UserDao;
import com.baizhi.dto.UserPageDTO;
import com.baizhi.entity.User;
import com.baizhi.po.UserPO;
import com.baizhi.po.UserPagePO;
import com.baizhi.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户(User)表服务实现类
 *
 * @author makejava
 * @since 2021-09-24 00:13:07
 */
@Service("userService")
@Transactional
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;  //对字符串支持比较友好,不能存储对象
    @Autowired
    private RedisTemplate redisTemplate;  //存储对象


    @Override
    public UserPagePO queryPage(UserPageDTO userPageDTO) {
        UserPagePO userPagePO = null;
        if (userPageDTO.getToken() != null) {
            System.out.println(userPageDTO.getToken());
            HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();
            String username = (String) hash.get(userPageDTO.getToken(), "username");
            System.out.println("username = " + username);
            if (username != null) {
                try {

                    Integer page1 = userPageDTO.getPage();
                    Integer pageSize = userPageDTO.getPer_page();

                    System.out.println("page1 = " + page1);
                    System.out.println("per_page = " + pageSize);

                    Page<User> page = new Page<>(page1, pageSize);
                    LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
                    //判断用户搜索条件是否为null，为null不执行，不为null加搜索条件
                    //id
                    if (userPageDTO.getId() != null) queryWrapper.eq(User::getId, userPageDTO.getId());
                    //name
                    if (userPageDTO.getName() != null) queryWrapper.eq(User::getName, userPageDTO.getName());
                    //phone
                    if (userPageDTO.getPhone() != null) queryWrapper.eq(User::getPhone, userPageDTO.getPhone());

                    Page<User> userPage = userDao.selectPage(page, queryWrapper);
                    long size = userPage.getTotal();
                    System.out.println("total = " + size);
                    //获取总条数
                    int i = new Long(size).intValue();

                    List<UserPO> userPOS = new ArrayList<>();

                    userPage.getRecords().forEach(user -> {
                        UserPO userPO = new UserPO();
                        BeanUtils.copyProperties(user, userPO);
                        userPOS.add(userPO);
                    });
                    userPagePO = new UserPagePO(i, userPOS);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return userPagePO;
    }

    @Override
    public UserPO queryById(Integer id) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, id);
        User user = userDao.selectOne(queryWrapper);
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(user, userPO);
        return userPO;
    }

    @Override
    public UserPO queryByName(String name) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getName, name);
        User user = userDao.selectOne(queryWrapper);
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(user, userPO);
        return userPO;
    }
}
